package net.anzhuo.controller.db.report;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.entity.PaginationVO;
import net.anzhuo.common.utils.RedisUtil;
import net.anzhuo.entity.GsBase;
import net.anzhuo.entity.GsOuttouzi;
import net.anzhuo.entity.db.outtouzi.Paging;
import net.anzhuo.entity.db.outtouzi.ResponseResult;
import net.anzhuo.entity.db.outtouzi.Result;
import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsOuttouziService;

@RestController
@Slf4j
public class OuttouziController {

	@Autowired
	private GsOuttouziService gsOuttouziService;
	@Autowired
	private GsBaseService gsBaseService;
	
	@RequestMapping("/report/outtouzi")
	public ResponseResult getOuttouzi(String keyWord, String pageSize) {
		log.info("===>getOuttouzi_keyWord="+keyWord+", pageSize="+pageSize);
		long startTime=System.currentTimeMillis();
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "50";
		}
	
		ResponseResult rr = null;
		try {
			rr = (ResponseResult)RedisUtil.get("report:outtouzi:"+keyWord);
			if (rr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" report:outtouzi 执行时间："+excTime+"s");
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		GsOuttouzi ssOuttouziQuery = new GsOuttouzi();
		GsBase base = gsBaseService.getByName(keyWord);
		ssOuttouziQuery.setCoMd5(base.getMd5());
		ssOuttouziQuery.setPageSize(Integer.parseInt(pageSize));
		PaginationVO<GsOuttouzi> outtouziList = gsOuttouziService.getListWithPage(ssOuttouziQuery);
		
		ResponseResult responseResult = new ResponseResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
		Paging paging = new Paging();
		paging.setPageIndex(1);
		paging.setPageSize(Integer.parseInt(pageSize));
		paging.setTotalRecords(outtouziList.getTotalCount());
		responseResult.setPaging(paging);
		
		List<Result> resultList = new ArrayList<>();
		Result result = null;
		if (outtouziList.getList()!=null && outtouziList.getList().size()>0) {
			for (GsOuttouzi o : outtouziList.getList()) {
				result = new Result();
				result.setName(o.getEnterpriseName());
				result.setNo("");
				GsBase b = gsBaseService.getByName(o.getEnterpriseName());
				result.setCreditCode(b.getCreditCode());
				result.setEconKind(b.getEnterpriseType());
				result.setStatus(b.getEnterpriseStatus());
				result.setRegistCapi(b.getRegZb());
				result.setOperName(b.getLegal());
				result.setFundedRatio(o.getInvestmentRatio());
				resultList.add(result);
			}
		}
		responseResult.setResult(resultList);
		
		try {
			RedisUtil.set("report:outtouzi:"+keyWord, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" report:outtouzi 执行时间："+excTime+"s");
		return responseResult;
	}
}
